/*
** Copyright (C) 2009 Zhang Cong <ftofficer@ftofficer.com>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**  
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**  
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H

#include <stdarg.h>
#include <stdio.h>

#include "liboi.h"
#include "liboi_log.h"

static log_func_t g_log_func = NULL;
static int g_log_level = LOG_NOLOG;

void oi_set_log_func(log_func_t log_func)
{
    g_log_func = log_func;
}

void oi_set_log_level(int log_level)
{
    g_log_level = log_level;
}

static void oi_log_vwrite(const char* file, int line,
                          int level, const char* fmt, va_list ap)
{
    char msg[4096] = {0};

    if ( ! g_log_func || level < g_log_level )
        return;

    vsnprintf(msg, sizeof(msg), fmt, ap);

    g_log_func(file, line, msg);
}

void oi_log_write(const char* file, int line, int level, const char* fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);

    oi_log_vwrite(file, line, level, fmt, ap);
}

